package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.constant.NumberConstants;
import com.rm.zeyou.entity.UserAddress;
import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.UserAddressMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.UserAddressService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.util.gaode.GaoDeUtil;
import com.rm.zeyou.vo.request.AddAddressVo;
import com.rm.zeyou.vo.request.GetAddressVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户地址表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:09
 */
@Slf4j
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {
    @Resource
    private GaoDeUtil gaoDeUtil;
    @Resource
    private UserAddressMapper userAddressMapper;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ApiRestResponse<Integer> addAddress(Long userId, AddAddressVo addVo) {
        UserAddress insertAddress = new UserAddress();
        insertAddress.setAddressName(addVo.getAddressName());
        insertAddress.setAddressTel(addVo.getAddressTel());
        insertAddress.setAddress(addVo.getAddress());
        insertAddress.setAddressDetails(addVo.getAddressDetails());
        insertAddress.setUserId(userId);
        insertAddress.setAddressType(addVo.getAddressType());
        int insert = userAddressMapper.insert(insertAddress);

        if (addVo.getAddressType().equals(NumberConstants.ZERO_CONSTANTS)) {
            // 将其它地址设置为正常
            List<UserAddress> addresses = userAddressMapper.selectList(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, userId));
            addresses.stream().filter(s -> !s.getId().equals(insertAddress.getId())).collect(Collectors.toList()).forEach(item -> {
                log.info("非默认地址:{}", item.getId());
                item.setAddressType(NumberConstants.ONE_CONSTANTS);
                userAddressMapper.updateById(item);
            });
        }
        return ApiRestResponse.success(insert);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ApiRestResponse<Integer> deleteAddress(Integer id) {
        return ApiRestResponse.success(userAddressMapper.deleteAddress(id));
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ApiRestResponse<Integer> updateAddress(Long userId, AddAddressVo updateVo) {
        UserAddress address = userAddressMapper.selectById(updateVo.getId());
        if (CheckEmptyUtil.isEmpty(address)) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.ADDRESS_ERROR);
        }
        if (!CheckEmptyUtil.isEmpty(updateVo.getAddressName())) {
            address.setAddressName(updateVo.getAddressName());
        }
        if (!CheckEmptyUtil.isEmpty(updateVo.getAddressTel())) {
            address.setAddressTel(updateVo.getAddressTel());
        }
        if (!CheckEmptyUtil.isEmpty(updateVo.getAddress())) {
            address.setAddress(updateVo.getAddress());
        }
        if (!CheckEmptyUtil.isEmpty(updateVo.getAddress())) {
            address.setAddressDetails(updateVo.getAddressDetails());
        }
        if (!CheckEmptyUtil.isEmpty(updateVo.getAddressType())) {
            if (updateVo.getAddressType().equals(NumberConstants.ZERO_CONSTANTS)) {
                // 将其它地址设置为正常
                List<UserAddress> addresses = userAddressMapper.selectList(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, userId));
                addresses.stream().filter(s -> !s.getId().equals(address.getId())).collect(Collectors.toList()).forEach(item -> {
                    log.info("非默认地址:{}", item.getId());
                    item.setAddressType(NumberConstants.ONE_CONSTANTS);
                    userAddressMapper.updateById(item);
                });
            }
            address.setAddressType(updateVo.getAddressType());
        }

        return ApiRestResponse.success(userAddressMapper.updateById(address));
    }

    @Override
    public ApiRestResponse<String> getAddress(GetAddressVo requestVo) {
        return ApiRestResponse.success(gaoDeUtil.ReGeoCode(requestVo.getLongitude() + "," + requestVo.getLatitude()));
    }

    @Override
    public ApiRestResponse<String> indexAddress(GetAddressVo requestVo) {
        if (CheckEmptyUtil.isEmpty(requestVo.getLongitude()) || CheckEmptyUtil.isEmpty(requestVo.getLatitude())){
            throw new ErrorCodeException(ErrorCodeExceptionEnum.LOCATION_ERROR);
        }
        String address = gaoDeUtil.GaoGetTownship(requestVo.getLongitude() + "," + requestVo.getLatitude());
        return ApiRestResponse.success(address);
    }

    @Override
    public ApiRestResponse<String> technicianAddress(GetAddressVo requestVo) {
        if (CheckEmptyUtil.isEmpty(requestVo.getLongitude()) || CheckEmptyUtil.isEmpty(requestVo.getLatitude())){
            throw new ErrorCodeException(ErrorCodeExceptionEnum.LOCATION_ERROR);
        }
        String address = gaoDeUtil.GaoGetCity(requestVo.getLongitude() + "," + requestVo.getLatitude());
        return ApiRestResponse.success(address);
    }
}
